﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using LinqLib.Sequence;

namespace PivotDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            ////Way 1:
            //var source = Generator.CreateContacts();
            //var result = source.Pivot(x => x.Phones, x => x.PhoneType, x => string.Concat("(", x.AreaCode, ") ", x.PhoneNumber), true).ToArray();
            //Console.ReadLine();

            //Way 2
            var data = ((new QTMTDaNangEntities()).tbKKXQ_Mau_KQ).ToList();
            
            var src = (from p in data
                       group p by new
                       {
                           Id = p.colMauID.ToString(),
                           Name = p.colSrcID.ToString()
                       } into gp
                       select new PivotRow<SelectionItem, string, double?>
                              {
                                  ObjectId = new SelectionItem(){Id = gp.Key.Id, Name = gp.Key.Name},
                                  Attributes = gp.Select(m => m.colParamID.ToString()),
                                  Values = gp.Select(g => g.colGTriDouble)
                              }).ToList();
            
            var paramIds = data.Select(c => c.colParamID.ToString()).ToList();

            var dataTable = PivotRow<SelectionItem, string, double?>.GetPivotTable(paramIds, src);

            foreach (DataColumn col in dataTable.Columns)
            {
                Console.Write("{0}\t", col.ColumnName);
            }
            Console.WriteLine();

            foreach (DataRow row in dataTable.Rows)
            {
                foreach (DataColumn col in dataTable.Columns)
                {
                    Console.Write("{0}\t", row[col.ColumnName]);
                }
                Console.WriteLine();
            }
            
            Console.ReadLine();
        }
    }

    public class Contact
    {
        public int Id;

        public string FirstName { get; set; }
        public string LastName { get; set; }

        public List<Phone> Phones { get; set; }
    }

    public class Phone
    {
        public Phone(string phoneType, string areaCode, string phoneNumber)
        {
            this.PhoneType = phoneType;
            this.AreaCode = areaCode;
            this.PhoneNumber = phoneNumber;
        }

        public string PhoneType { get; set; }
        public string AreaCode { get; set; }
        public string PhoneNumber { get; set; }
    }

    public static class Generator
    {
        public static List<Contact> CreateContacts()
        {
            return new List<Contact>
                {
                    new Contact
                        {
                            Id = 1,
                            FirstName = "John",
                            LastName = "Doe",
                            Phones = new List<Phone>
                                {
                                    new Phone("Home", "305", "555-1111"),
                                    new Phone("Office", "305", "555-2222"),
                                    new Phone("Cell", "305", "555-3333")
                                }
                        },
                    new Contact
                        {
                            Id = 2,
                            FirstName = "Jane",
                            LastName = "Doe",
                            Phones = new List<Phone>
                                {
                                    new Phone("Home", "305", "555-1111"),
                                    new Phone("Office", "305", "555-4444"),
                                    new Phone("Cell", "305", "555-5555")
                                }
                        },
                    new Contact
                        {
                            Id = 3,
                            FirstName = "Jerome",
                            LastName = "Doe",
                            Phones = new List<Phone>
                                {
                                    new Phone("Home", "305", "555-6666"),
                                    new Phone("Office", "305", "555-2222"),
                                    new Phone("Cell", "305", "555-7777")
                                }
                        },
                    new Contact
                        {
                            Id = 4,
                            FirstName = "Joel",
                            LastName = "Smith",
                            Phones = new List<Phone>
                                {
                                    new Phone("Fax", "305", "555-6666"),
                                    new Phone("Office", "305", "555-2222"),
                                    new Phone("Cell2", "305", "555-7778"),
                                    new Phone("Cell", "305", "555-7777")
                                }
                        }
                };
        }
    }

    public class SelectionItem
    {
        public string Id { get; set;}

        public string Name { get; set; }
    }
}
